package keeto.com.vn.classified.utils;

import keeto.com.vn.classified.config.IConfig;
import keeto.com.vn.classified.config.IConstant;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.ui.R;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

public class StatusMessageUtil implements IConfig, IConstant {

	private Context mContext = null;
	private static StatusMessageUtil sInstance = null;
	
	private StatusMessageUtil(Context context) {
		this.mContext = context;
	}

	public static StatusMessageUtil getInstance(Context context) {
		if(sInstance==null){
			sInstance = new StatusMessageUtil(context);
		}
		return sInstance;
	}

	/**
	 * warning for extend
	 * 
	 * @param returnCode
	 * @param action
	 * @return
	 */
	public  String getStatusMessageByStatus(int returnCode) {
		String returnStr = ""+returnCode;
		switch (returnCode) {
		case IProtocol.STATUS_CODE_OK_200:
			returnStr = getString(R.string.msg_ok);
			break;
		case IProtocol.STATUS_CODE_AUTHENTICATION_FAIL_403:
			returnStr = getString(R.string.msg_auth_failed);
			break;
		case IProtocol.STATUS_CODE_ROSTER_NOT_AUTHORIZED_TO_CHECK_STATUS_404:
			returnStr = getString(R.string.msg_roster_not_authorized_to_check_status);
			break;
		case IProtocol.STATUS_CODE_SESSIOON_TIMEOUT_408:
			returnStr = getString(R.string.msg_session_timeout);
			break;
		case IProtocol.STATUS_CODE_GENERICFAIL_500:
			returnStr = getString(R.string.msg_generic_fail);
			break;
		case IProtocol.STATUS_CODE_ROSTER_ALREADY_EXIST_420:
			returnStr = getString(R.string.msg_roster_already_exist);
			break;
		case IProtocol.STATUS_CODE_ROSTER_NOT_EXIST_424:
			returnStr = getString(R.string.msg_roster_not_existed);
			break;
		case IProtocol.STATUS_CODE_NETWORKFAIL_501:
			returnStr = getString(R.string.msg_networkfail);
			break;
		case IProtocol.STATUS_CODE_REGISTER_USER_EXSIT_600:
			returnStr = getString(R.string.msg_account_exsited);
			break;
		case IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502:
			returnStr = getString(R.string.msg_connection_timeout);
			break;
		case IProtocol.STATUS_CODE_CANT_ADD_SELF_611:
			returnStr = getString(R.string.msg_connection_cannot_add_self);
			break;
		}
		return returnStr;
	}

	public  String getMessageByRegisterResponse(int returnCode) {
		if (GW_REGISTER_SUCCESS == returnCode) {
			return getString(R.string.status_gateway_service_ok);
		} else if (GW_REGISTER_AUTHENTICATE_FAIL == returnCode) {
			return getString(R.string.status_gateway_service_authenticate_fail);
		} else if (GW_REGISTER_OTHER_REASON_FAIL == returnCode) {
			return getString(R.string.status_gateway_service_other_reason_fail);
		} else {
			return getString(R.string.gateway_service_not_added);
		}
	}
	
	public  String getMessageByUnregisterResponse(int returnCode) {
		if (GW_UNREGISTER_SUCCESS == returnCode) {
			return getString(R.string.msg_gateway_unregister_ok);
		}else if(GW_UNREGISTER_FAIL == returnCode){
			return getString(R.string.msg_gateway_unregister_failed);
		} 
		return "";
	}

	public String getStatusByKey(String key, boolean isConnected) {
		
		if(isConnected){
			
			if (key.equals(IConstant.ONLINE)) {
				return getString(R.string.status_online);
			} else if (key.equals(XA)) {
				return getString(R.string.status_idle);
			} else if (key.equals(BUSY)) {
				return getString(R.string.status_busy);
			} else if (key.equals(AWAY)) {
				return getString(R.string.status_away);
			} else if (key.equals(HIDE)) {
				return getString(R.string.status_hide);
			} else if (key.equals(OFFLINE)) {
				return getString(R.string.status_offline);
			} else if (key.equals(GROUP)){
				return getString(R.string.separator_group);
			}else {
				return getString(R.string.status_offline);
			}
			
		}else{
			return getString(R.string.status_offline);
		}
		
	}
	
	

	public String getGroupExbyKey(String key){
		if (key.equals(IConstant.BUDDY_GROUP)){
			return getString(R.string.buddy_group);
		}else if (key.equals(IConstant.BUDDY_KEETO)){
			return getString(R.string.buddy_keeto);
		}else if (key.equals(IConstant.BUDDY_YAHOO)){
			return getString(R.string.buddy_yahoo);
		}else if (key.equals(IConstant.BUDDY_HANGOUT)){
			return getString(R.string.buddy_hangout);
		}else {
			return "";
		}
	}
	public String getMessageByGatewayLogon(int returnCode) {
		switch (returnCode) {
		case GW_LOGIN_ALREADY:
			return getString(R.string.msg_gateway_logon_already);
		case GW_LOGIN_AUTHENTICATE_FAIL:
			return getString(R.string.msg_gateway_logon_auth_fail);
		case GW_LOGIN_OTHER_REASON_FAIL:
			return getString(R.string.msg_gateway_logon_other_fail);
		}
		return getString(R.string.msg_gateway_logon_other_fail);
	}

	public  String getLogoffMessageByResultCode(int returnCode) {
		switch (returnCode) {
		// User sends logoff request and logoff successfully
		case IConstant.GW_ACTIVE_LOGOFF_SUCCESS:
			return getString(R.string.gw_active_logoff_success);
			// User sends logoff request but logoff fails
		case IConstant.GW_ACTIVE_LOGOFF_FAIL:
			return getString(R.string.gw_active_logoff_fail);
			// Auto logoff because someone login from another location.
		case IConstant.GW_PASSIVE_LOGOFF_OTHER_LOCATION:
			return getString(R.string.gw_passive_logoff_other_location);
			// Auto logoff because server shuts down. This value is only used by
			// MSN
		case IConstant.GW_PASSIVE_LOGOFF_SERVER_SHUTDOWN:
			return getString(R.string.gw_passive_logoff_server_shutdown);
			// Auto logoff because of other reason, such as network errors.
		case IConstant.GW_PASSIVE_LOGOFF_OTHER_REASON:
			return getString(R.string.gw_passive_logoff_other_reason);
			// Already logoff
		case IConstant.GW_PASSIVE_LOGOFF_ALREADY:
			return getString(R.string.gw_passive_logoff_already);
			
		}
		return "";
	}

	public  String getString(int id) {
		return mContext.getResources().getString(id);
	}

	public  void showCustomWarningToast(Activity a, String message,
			int level) {
		
		/*
		LayoutInflater inflater = a.getLayoutInflater();
		View view = inflater.inflate(R.layout.toast_warning_view, null);
		TextView label = (TextView) view.findViewById(R.id.toast_warning_text);
		label.setText(message);
		Toast toast = new Toast(a);
		toast.setView(view);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.show();
		*/
		Toast.makeText(mContext, message, Toast.LENGTH_LONG);
	}

}
